<?php

namespace app\common\model\dynamic;


use addons\xyb\XypHelper;
use app\common\model\daily\UsersModel;
use think\Model;


class Progress extends Model
{

    // 表名
    protected $name = 'dynamic_progress';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    public static $grant_status_list = [
        1 => '进行中',
        2 => '已结束',
        3 => '已下架'
    ];
    public static $grant_method_list = [
        1 => '全部',
        2 => '指定区域',
        3 => '指定用户',
    ];

    protected $append = [

    ];

    public function getJobsInfoAttr($value, $data){
        if(isset($data['jobs_id']) && $data['jobs_id']){
            return Jobs::where('id', $data['jobs_id'])->find();
        } else {
            return '';
        }
    }
    public function getUserCountAttr($value, $data){
        if(isset($data['id']) && $data['id']){
            return self::where('progress_id', $data['id'])->where('progress_type', 2)->count();
        } else {
            return 0;
        }
    }
    public function getCreateDateAttr($value, $data){
        if(isset($data['createtime']) && $data['createtime']){
            return date('Y-m-d H:i:s', $data['createtime']);
        } else {
            return '';
        }
    }
    public function getDeadlineDateAttr($value, $data){
        if(isset($data['deadline_date']) && $data['deadline_date']){
            return date('Y-m-d H:i:s', $data['deadline_date']);
        } else {
            return '';
        }
    }

    public static function getProgressExamineLists(){
        $deadline = request()->param('deadline/a');
        $grant_status = request()->param('grant_status/a');
        $field = 'id,user_id,activity_id,jobs_id,progress_type,grant_status,deadline_time,trends_ids,
        weigh as jobs_info,type as user_count,createtime as create_date,createtime, deadline_time as deadline_date,
        complete_time as complete_date';
        $query = self::field($field);
        if($grant_status = XypHelper::stringToArray($grant_status)){
            $query->whereIn('grant_status', $grant_status);
        }

        $list = $query->order('id','desc')->paginate();
    }
    public static function getProgressAdminLists(){

        $deadline = request()->param('deadline/a');
        $grant_status = request()->param('grant_status/a');
        $field = 'id,user_id,activity_id,jobs_id,progress_type,grant_status,deadline_time,complete_time,
        weigh as jobs_info,type as user_count,createtime as create_date,createtime, deadline_time as deadline_date,
        complete_time as complete_date';
        $query = self::field($field)->where('progress_type',1);
        if($grant_status = XypHelper::stringToArray($grant_status)){
            $query->whereIn('grant_status', $grant_status);
        }

        $list = $query->order('id','desc')->paginate();
        return $list;
    }

    public static function getParticipateDifferenceUsers($progress_id, $grant_total = 4){
        $total = self::where('progress_id', $progress_id)->where('progress_type',2)->count();
        $difference =  (int)($grant_total - $total);
        return $difference > 0 ? $difference : 0;
    }

    public static function getParticipateUsersList($progress_id, $grant_total = 0){

        $filed = 'id,user_id,assistant_time';
        $list = self::field($filed)->where('progress_id', $progress_id)->where('progress_type',2)->select();
        foreach ($list as $key => $value){
            $users = UsersModel::field('id,nickname,avatar')->where('id', $value['user_id'])->find();
            $list[$key]['nickname'] = $users['nickname'];
            $list[$key]['avatar'] = $users['avatar'];
        }

        $lists_maps = [];
        for ($i = 0; $i < $grant_total; $i++){
            if (!isset($list[$i]) || !($list[$i])){
                $lists_maps[$i] = [
                    'assistant_time'    => 0,
                    'user_id' => 0,
                    'nickname' => '',
                    'avatar' => '',
                ];
            }else {
                $lists_maps[$i]['assistant_time'] = $list[$i]['assistant_time'] ? $list[$i]['assistant_time'] : '';
                $lists_maps[$i]['user_id'] = $list[$i]['user_id'] ? $list[$i]['user_id'] : '';
                $lists_maps[$i]['nickname'] = $list[$i]['nickname'] ? $list[$i]['nickname'] : '';
                $lists_maps[$i]['avatar'] = $list[$i]['avatar'] ? cdnurl($list[$i]['avatar']) : '';
            }
        }
        return array_slice($lists_maps, 0, $grant_total);
    }

    public static function userRegistrationParticipate($user_id, $jobs){
        $forms = [
            'activity_id'       => $jobs->activity_id,
            'jobs_id'           => $jobs->id,
            'progress_id'       => 0,
            'user_id'           => $user_id,
            'progress_type'     => 1,
            'grant_status'      => 1,
            'status'            => 1,
            'createtime'        => time(),
            'deadline_time'     => time() + ($jobs->days_num * 3600),
        ];
        return self::create($forms);
    }

    public static function userPeopleRegistrationParticipate($user_id, $progress,$fu_task_status = 2){
        $forms = [
            'activity_id'       => $progress->activity_id,
            'jobs_id'           => $progress->jobs_id,
            'progress_id'       => $progress->id,
            'user_id'           => $user_id,
            'progress_type'     => 2,
            'grant_status'      => $fu_task_status,
            'status'            => 2,
            'createtime'        => time(),
            'assistant_time'    => date('Y-m-d H:i:s'),
            'deadline_time'     => $progress->deadline_time,
        ];
        return self::create($forms);
    }

    public static function setProgressCompletion($progress_id, $push_talk_ids = '', $push_trends_ids = ''){
        $progress = self::where('id',$progress_id)->find();
        if(!$progress){
            return false;
        }
        $progress->grant_status = 2;
        $progress->status = 2;
        $progress->complete_time = date('Y-m-d H:i:s');
        $progress->examine_time = date('Y-m-d H:i:s');
        if($push_talk_ids){
            $progress->talk_id = XypHelper::formatArrayToString($push_talk_ids);
        }
        if($push_trends_ids){
            $progress->trends_ids = XypHelper::formatArrayToString($push_trends_ids);
        }
        $progress->save();
        Jobs::where('id', $progress->jobs_id)->setInc('complete_total');
  //      Jobs::where('id', $progress->jobs_id)->setDec('progress_total');
        return true;
    }
}